/**
 * 数组map函数
 * @param {Array} array
 * @param {Function} callback
 */
export function map(arr, callback) {
  const result = []
  for (let i = 0, length = arr.length; i < length; ++i) {
    result.push(callback(arr[i]), i)
  }
  return result
}

/**
 * 数组reduce方法
 * @param {Array} arr
 * @param {Function} callback
 */
export function reduce(arr, callback, startValue) {
  let result = startValue
  for (let i = 0, length = arr.length; i < length; ++i) {
    result = callback(result, arr[i], i)
  }
  return result
}

/**
 * 数组过滤器
 * @param {Array} arr
 * @param {Function} callback
 */
export function filter(arr, callback) {
  const result = []
  for (let i = 0, length = arr.length; i < length; ++i) {
    if (callback(arr[i], i)) {
      result.push(arr[i])
    }
  }
  return result
}

/**
 * 数组find方法
 * @param {Array} arr
 * @param {Function} callback
 */
export function find(arr, callback) {
  let res = null
  for (let i = 0, length = arr.length; i < length; ++i) {
    if (callback(arr[i], i)) {
      res = arr[i]
      break
    }
  }
  return res
}

/**
 * 数组findIndex方法
 * @param {Array} arr
 * @param {Function} callback
 */
export function findIndex(arr, callback) {
  let index = -1
  for (let i = 0, length = arr.length; i < length; ++i) {
    if (callback(arr[i], i)) {
      index = i
      break
    }
  }
  return index
}

/**
 * 数组every方法
 * @param {Array} arr
 * @param {Function} callback
 */
export function every(arr, callback) {
  for (let i = 0, length = arr.length; i < length; ++i) {
    if (!callback(arr[i], i)) {
      return false
    }
  }
  return true
}

/**
 * 数组some方法
 * @param {Array} arr
 * @param {Function} callback
 */
export function some(arr, callback) {
  for (let i = 0, length = arr.length; i < length; ++i) {
    if (callback(arr[i], i)) {
      return true
    }
  }
  return false
}
